using System.Collections.Generic;
using Naptime.Extensions;
using TMPro;
using UnityEngine;
using UnityEngine.SceneManagement;
using UnityEngine.UI;
using Voodoo.Pattern;

public class SettingsPanel : MonoBehaviour
{
	[SerializeField]
	private GameObject settingBtn;

	[SerializeField]
	private GameObject settingsPanel;

	[SerializeField]
	private Button sfxBtn;

	private GameProperties gameProperties;

	[SerializeField]
	private Button musicBtn;

	[SerializeField]
	private Button vibrationBtn;

	[SerializeField]
	private Button mainMenuBtn;

	[SerializeField]
	private Button startMenuBtn;

	[SerializeField]
	private Button resolutionBtn;

	[SerializeField]
	private Button fullscreenBtn;

	[SerializeField]
	private Button closeBtn;

	[SerializeField]
	private TextMeshProUGUI sfxTxt;

	[SerializeField]
	private TextMeshProUGUI musicTxt;

	[SerializeField]
	private TextMeshProUGUI vibrationTxt;

	[SerializeField]
	private TextMeshProUGUI resolutionTxt;

	[SerializeField]
	private TextMeshProUGUI fullscreenTxt;

	[SerializeField]
	private List<ButtonWithIcon> btns;

	[SerializeField]
	private StartMenu startMenu;

	private int btnInd;

	private bool delay;

	private float timer;

	private bool panelActive;

	private bool isFromMainMenu;

	private bool fromGame;

	private List<GameObject> _activeObjects = new List<GameObject>();

	private int BtnIndex
	{
		get
		{
			return btnInd;
		}
		set
		{
			if (delay)
			{
				delay = false;
				int num = value;
				if (value < 0)
				{
					num = btns.Count - 1;
				}
				else if (value > btns.Count - 1)
				{
					num = 0;
				}
				if (!btns[num].gameObject.activeSelf)
				{
					ShowActiveIcon(btnInd);
					return;
				}
				btnInd = num;
				ShowActiveIcon(num);
			}
		}
	}

	private void OnDisable()
	{
		startMenu?.SetActive(active: true);
	}

	public void Setup(bool isMainMenu = false)
	{
		MyAdManager.Instance.MyShowNative(0f);
		if ((settingBtn != null && !settingBtn.gameObject.activeSelf) || (settingsPanel.activeSelf && SceneManager.GetActiveScene().name == "GamePlay"))
		{
			return;
		}
		isFromMainMenu = isMainMenu;
		settingsPanel.SetActive(value: true);
		delay = true;
		timer = 0f;
		BtnIndex = 0;
		panelActive = true;
		sfxBtn.onClick.RemoveAllListeners();
		musicBtn.onClick.RemoveAllListeners();
		vibrationBtn.onClick.RemoveAllListeners();
		closeBtn.onClick.RemoveAllListeners();
		resolutionBtn?.onClick.RemoveAllListeners();
		fullscreenBtn?.onClick.RemoveAllListeners();
		_activeObjects.Clear();
		if (!isMainMenu)
		{
			if (!gameProperties)
			{
				gameProperties = GameObject.Find("Manager").GetComponent<GameProperties>();
			}
			if (SingletonMB<GameManager>.Instance != null)
			{
				SingletonMB<GameManager>.Instance.AnyWindowActive = true;
			}
			if (SingletonMB<UIManager>.Instance != null)
			{
				if (SingletonMB<UIManager>.Instance.FuelIndicator.activeSelf)
				{
					_activeObjects.Add(SingletonMB<UIManager>.Instance.FuelIndicator);
					SingletonMB<UIManager>.Instance.FuelIndicator.SetActive(value: false);
				}
				if (SingletonMB<UIManager>.Instance.skinsBtn.gameObject.activeSelf)
				{
					_activeObjects.Add(SingletonMB<UIManager>.Instance.skinsBtn.gameObject);
					SingletonMB<UIManager>.Instance.skinsBtn.SetActive(active: false);
				}
				if (SingletonMB<UIManager>.Instance.upgradesBtn.gameObject.activeSelf)
				{
					_activeObjects.Add(SingletonMB<UIManager>.Instance.upgradesBtn.gameObject);
					SingletonMB<UIManager>.Instance.upgradesBtn.SetActive(active: false);
				}
				if (SingletonMB<UIManager>.Instance.playBtn.gameObject.activeSelf)
				{
					_activeObjects.Add(SingletonMB<UIManager>.Instance.playBtn.gameObject);
					SingletonMB<UIManager>.Instance.playBtn.SetActive(active: false);
				}
				if (SingletonMB<UIManager>.Instance.prestigeBtn.gameObject.activeSelf)
				{
					_activeObjects.Add(SingletonMB<UIManager>.Instance.prestigeBtn.gameObject);
					SingletonMB<UIManager>.Instance.prestigeBtn.SetActive(active: false);
				}
				if (SingletonMB<UIManager>.Instance.prestigePanel.activeSelf)
				{
					_activeObjects.Add(SingletonMB<UIManager>.Instance.prestigePanel);
					SingletonMB<UIManager>.Instance.prestigePanel.SetActive(value: false);
				}
				if (SingletonMB<UIManager>.Instance.prestigeShop.activeSelf)
				{
					_activeObjects.Add(SingletonMB<UIManager>.Instance.prestigeShop);
					SingletonMB<UIManager>.Instance.prestigeShop.SetActive(value: false);
				}
				if (SingletonMB<UIManager>.Instance.skinShop.activeSelf)
				{
					_activeObjects.Add(SingletonMB<UIManager>.Instance.skinShop);
					SingletonMB<UIManager>.Instance.skinShop.SetActive(value: false);
				}
				SingletonMB<UIManager>.Instance.HideLeftBtns();
				SingletonMB<UIManager>.Instance.HideRightBtns();
			}
			if (SingletonMB<GameManager>.Instance != null)
			{
				if (SingletonMB<GameManager>.Instance.canMove)
				{
					fromGame = true;
					startMenuBtn.gameObject.SetActive(value: false);
					mainMenuBtn.gameObject.SetActive(value: true);
					mainMenuBtn.onClick.RemoveAllListeners();
					mainMenuBtn.onClick.AddListener(delegate
					{
						SceneManager.LoadScene("GamePlay");
					});
				}
				else
				{
					fromGame = false;
					mainMenuBtn.gameObject.SetActive(value: false);
					startMenuBtn.gameObject.SetActive(value: true);
					startMenuBtn.onClick.RemoveAllListeners();
					startMenuBtn.onClick.AddListener(delegate
					{
						SceneManager.LoadScene("MainMenu");
					});
				}
			}
			closeBtn.onClick.AddListener(Close);
		}
		else if (!gameProperties)
		{
			gameProperties = GetComponent<GameProperties>();
		}
		sfxTxt.text = (gameProperties.IsSfxEnable() ? "ON" : "OFF");
		musicTxt.text = (gameProperties.IsSoundEnable() ? "ON" : "OFF");
		if (vibrationTxt != null)
		{
			vibrationTxt.text = (gameProperties.IsHapticEnable() ? "ON" : "OFF");
		}
		if (fullscreenTxt != null)
		{
			fullscreenTxt.text = (gameProperties.IsFullScreen() ? "ON" : "OFF");
		}
		if (resolutionTxt != null)
		{
			resolutionTxt.text = gameProperties.GetResolution();
		}
		sfxBtn.onClick.AddListener(Sfx);
		musicBtn.onClick.AddListener(Music);
		vibrationBtn.onClick.AddListener(Vibtarions);
		fullscreenBtn?.onClick.AddListener(ToggleFullscreen);
		resolutionBtn?.onClick.AddListener(ChangeResolution);
	}

	private void ShowActiveIcon(int activeBtnInd)
	{
		foreach (ButtonWithIcon btn in btns)
		{
			btn.HideActiveIcon();
		}
		btns[activeBtnInd].ShowActiveIcon();
	}

	private void OpenMenu()
	{
		if (!base.gameObject.activeSelf)
		{
			startMenu.SetActive(active: true);
		}
	}

	public void Sfx()
	{
		gameProperties.SetSfxEnable(!gameProperties.IsSfxEnable());
		sfxTxt.text = (gameProperties.IsSfxEnable() ? "ON" : "OFF");
	}

	public void Music()
	{
		gameProperties.SetSoundEnable(!gameProperties.IsSoundEnable());
		musicTxt.text = (gameProperties.IsSoundEnable() ? "ON" : "OFF");
	}

	public void Vibtarions()
	{
		gameProperties.SetHapticEnable(!gameProperties.IsHapticEnable());
		vibrationTxt.text = (gameProperties.IsHapticEnable() ? "ON" : "OFF");
	}

	public void ToggleFullscreen()
	{
		gameProperties.SetFullScreen(!gameProperties.IsFullScreen());
		fullscreenTxt.text = (gameProperties.IsFullScreen() ? "ON" : "OFF");
	}

	public void ChangeResolution()
	{
		gameProperties.SetNextResolution();
		resolutionTxt.text = gameProperties.GetResolution();
	}

	private void Close()
	{
		panelActive = false;
		settingsPanel.SetActive(value: false);
		foreach (GameObject activeObject in _activeObjects)
		{
			activeObject.SetActive(value: true);
		}
		_activeObjects.Clear();
		if (!(SceneManager.GetActiveScene().name == "MainMenu"))
		{
			if (SingletonMB<GameManager>.Instance != null)
			{
				SingletonMB<GameManager>.Instance.AnyWindowActive = false;
			}
			Invoke("CheckOilLevel", 0.001f);
		}
	}

	private void CheckOilLevel()
	{
		if (SingletonMB<UIManager>.Instance.prestigePanel.activeSelf || SingletonMB<UIManager>.Instance.prestigeShop.activeSelf || SingletonMB<UIManager>.Instance.skinShop.activeSelf)
		{
			SingletonMB<UIManager>.Instance.FuelIndicator.SetActive(value: false);
		}
	}

	private void ShowMenuPanel()
	{
		settingsPanel.SetActive(value: false);
		startMenu.Setup();
	}

	private void Update()
	{
		if (Singleton<InputController>.Instance.Plus)
		{
			Setup();
		}
		if (!panelActive)
		{
			return;
		}
		if (!isFromMainMenu)
		{
			if (Singleton<InputController>.Instance.X)
			{
				if (fromGame)
				{
					mainMenuBtn.onClick.Invoke();
				}
				else
				{
					startMenuBtn.onClick.Invoke();
				}
			}
			if (Singleton<InputController>.Instance.Back)
			{
				Close();
			}
		}
		else if (Singleton<InputController>.Instance.Back)
		{
			ShowMenuPanel();
		}
		if (Singleton<InputController>.Instance.Action)
		{
			btns[BtnIndex].btn.onClick.Invoke();
		}
		if (Singleton<InputController>.Instance.Up)
		{
			BtnIndex--;
			delay = true;
		}
		if (Singleton<InputController>.Instance.Down)
		{
			BtnIndex++;
			delay = true;
		}
		if (Singleton<InputController>.Instance.Vertical == 0f)
		{
			delay = true;
			timer = 0f;
		}
		else if (Singleton<InputController>.Instance.Vertical < 0f)
		{
			BtnIndex++;
		}
		else if (Singleton<InputController>.Instance.Vertical > 0f)
		{
			BtnIndex--;
		}
		if (!delay)
		{
			timer += Time.deltaTime;
			if (timer > 0.3f)
			{
				delay = true;
				timer = 0f;
			}
		}
	}
}
